package socketDemo1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class SocketDemo2 {
    public static void main(String[] args) throws IOException {
        //1.创建接收对象
        int port=10086;
        DatagramSocket re = new DatagramSocket(port);
        //2.接收数据
        byte message[]=new byte[1024];
        DatagramPacket packet = new DatagramPacket(message,message.length);
//        re.receive(packet);
//        byte[] data = packet.getData();
//        InetAddress address = packet.
//                 getAddress();
//        int length = packet.getLength();
//        int port1 = packet.getPort();
//        String ms = new String(data, 0, length);
//        System.out.println("接收到的数据:"+new String(data,0,length));
//        System.out.println("这是来自"+address+"地址的,端口号为"+port1+"发来的");
        while(true){
            re.receive(packet);
            byte[] data = packet.getData();
            InetAddress address = packet.getAddress();
            int length = packet.getLength();
            int port1 = packet.getPort();
            String ms = new String(data, 0, length);
           if(ms.equals("--"))
               break;
            System.out.println("接收到的数据:"+new String(data,0,length));
            System.out.println("这是来自"+address+"地址的,端口号为"+port1+"发来的");
        }
        re.close();
    }
}
